package code;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

class Score {
	String name;
	int id;
	int chinese;
	int math;
	int english;
	int physical;
	int chemistry;
	public int sumscore() {
		return (this.chinese + this.math + this.english + this.physical + this.chemistry);
	}
}

@SuppressWarnings("rawtypes")
class ScoreComparator implements Comparator {
	public int compare(Object a, Object b) {
		Score c = (Score)a;
		Score d = (Score)b;
		if(c.sumscore() > d.sumscore()) return 1;
		else if(c.sumscore() == d.sumscore()) return 0;
		else
			return -1;
	}
}

public class FileTest {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File firstFile = new File("src\\txt\\scorelist.txt");
		File secondFile = new File("src\\txt\\studentlist.txt");
		File result = new File("src\\txt\\result.txt");
		
		List<Score> list = new ArrayList<>();
		try {
			FileReader fr = new FileReader(firstFile);
			BufferedReader bufr = new BufferedReader(fr);
			String s = null;
			while((s = bufr.readLine()) != null) {
				String[] temp = s.split("\t");
				Score score = new Score();
				score.name = temp[0];
				score.id = Integer.parseInt(temp[1]);
				score.chinese = Integer.parseInt(temp[2]);
				score.math = Integer.parseInt(temp[3]);
				score.english = Integer.parseInt(temp[4]);
				score.physical = Integer.parseInt(temp[5]);
				score.chemistry = Integer.parseInt(temp[6]);
				list.add(score);
				}
			
			fr = new FileReader(secondFile);
			bufr = new BufferedReader(fr);
			s = null;
			FileWriter fw = new FileWriter(firstFile, true);
			BufferedWriter bufw = new BufferedWriter(fw);
			Random r = new Random();
			while((s = bufr.readLine()) != null) {
				String[] temp = s.split("\t");
				Score score = new Score();
				score.name = temp[0];
				score.id = Integer.parseInt(temp[1]);
				score.chinese = (int) (Math.sqrt(100) * r.nextGaussian() + 75);
				score.math = (int) (Math.sqrt(100) * r.nextGaussian() + 75);
				score.english = (int) (Math.sqrt(100) * r.nextGaussian() + 75);
				score.physical = (int) (Math.sqrt(100) * r.nextGaussian() + 75);
				score.chemistry = (int) (Math.sqrt(100) * r.nextGaussian() + 75);
				bufw.write(score.name + "\t" + score.id + "\t" + score.chinese + "\t" + score.math + "\t" + score.english + "\t" + score.physical + "\t" + score.chemistry + "\n");
				list.add(score);;	
			}
			
			bufw.close();
			fw.close();
			bufr.close();
			fr.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int[] sum = {0, 0, 0, 0, 0};
		String[] subject = {"语文","数学","英语","物理","化学"};
		int[] average = {0, 0, 0, 0, 0};
		for(Score s:list) {
			sum[0] += s.chinese;
			sum[1] += s.math;
			sum[2] += s.english;
			sum[3] += s.physical;
			sum[4] += s.chemistry;
			System.out.println(s.name + "总成绩为:" + s.sumscore());
		}
		for(int i = 0; i < 5; i++) {
			average[i] = (int)(sum[i]/list.size());
		}
		for(int j = 0; j < 5; j++) {
			System.out.println(subject[j] + "平均成绩为:" + average[j]);
		}
		
		//排序
		Collections.sort(list, new ScoreComparator());
		
		FileWriter frw;
		try {
			frw = new FileWriter(result, false);
			BufferedWriter bufrw = new BufferedWriter(frw);
			
			for(Score score:list) {
				bufrw.write(score.name + "\t" + score.id + "\t" + score.chinese + "\t" + score.math + "\t" + score.english + "\t" + score.physical + "\t" + score.chemistry + "\t" + score.sumscore() + "\n");
			}
			
			bufrw.close();
			frw.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
